package com.int_tech.newTimeTask;

import com.int_tech.control.dao.SocketLogSendDao;
import com.int_tech.control.entity.SocketLogSend;
import com.int_tech.messionLog.dao.MessionLogDao;
import com.int_tech.messionLog.entity.MessionLog;
import com.int_tech.utils.DateTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * Created by liubch on 2018/5/27.
 */
@Component
public class CSocketLogSendToMessionLogTimeTask {

    @Autowired
    private SocketLogSendDao socketLogSendDao;

    @Autowired
    private MessionLogDao messionLogDao;



    //存储日志
    @Scheduled(cron="0/10 * * * * ?")
    public void checkMessionLogInView(){

        String dataTime = DateTool.Date2String(new Date(),"yyyy-MM");

        dataTime = dataTime + "-01 00:00:00";


        Date date = DateTool.string2Date(dataTime,"yyyy-MM-dd HH:mm:ss");

        List<SocketLogSend> socketLogSendList = socketLogSendDao.findByServiceTypeAndCreateTimeAfter("M",date);

        for(SocketLogSend socketLogSend : socketLogSendList)
        {
            try
            {
                socketLogSend.setServiceType("M-1");

                socketLogSendDao.save(socketLogSend);

                if("A0".equals(socketLogSend.getCode())||"A2".equals(socketLogSend.getCode())||"A4".equals(socketLogSend.getCode()))
                {
                    MessionLog messionLog = new MessionLog();

                    String logData = socketLogSend.getDataString();

                    String[] logs = logData.split(",");

                    //经纬度
                    String slng = logs[7];
                    String slat = logs[8];

                    //时间
                    String rtcTime = logs[6];
                    String gepTime = logs[9];

                    String carNumber = logs[2];


                    messionLog.setCarNumber(carNumber);
                    messionLog.setRtcTime(rtcTime);
                    messionLog.setGpsIsFail(0);
                    messionLog.setGepUploadTime(gepTime);
                    messionLog.setLat(slat);
                    messionLog.setLng(slng);
//                    messionLogShow.setMessionNumber(mession.getMessionNumber());
                    messionLog.setCreateTime(socketLogSend.getCreateTime());
                    messionLog.setDoorMessage("close");
//                    messionLogShow.setMessionCount(String.valueOf(mession.getMessionCount()));

                    messionLogDao.save(messionLog);
                }
            }
            catch (Exception e)
            {
                socketLogSend.setServiceType("M-1");

                socketLogSendDao.save(socketLogSend);
            }
        }
    }
}
